﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Collections;

namespace ElectronicJournal
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Image1.ImageUrl = "~/Images/Header/"+returnImageHeader();
            leftSideBar();
            SpamLord sLord = new SpamLord();

            var currentUser = Membership.GetUser();
            if (currentUser != null)
            {
                LoginAuthorReviewerDiv.Visible = true;
            //    EditorDiv.Visible = true;

                if (Roles.IsUserInRole(currentUser.UserName, "Author"))
                {
                    liAuthor.Style.Add("display","block");
                   // liAuthor.Style.Add("color","red");
                 //  EditorDiv.Visible = false;
   


                    //HtmlGenericControl li = new HtmlGenericControl("li");
                    //HtmlGenericControl anchor = new HtmlGenericControl("a");
                    //anchor.Attributes.Add("href", "/Account/AuthorAccount/MyAccount.aspx");
                    //anchor.Attributes.Add("ID", "authorsAnchor");
                    //anchor.InnerText = "Публикации";
                    //li.Controls.Add(anchor);
                    //Authors.Controls.Add(li);
                }
                else if (Roles.IsUserInRole(currentUser.UserName, "Reviewer"))
                {
                    liReviewer.Style.Add("display", "block");
                  //  EditorDiv.Visible = false;
                    //HtmlGenericControl li = new HtmlGenericControl("li");
                    //HtmlGenericControl anchor = new HtmlGenericControl("a");
                    //anchor.Attributes.Add("href", "/Account/ReviewerAccount/Reviewer.aspx");
                    //anchor.Attributes.Add("ID", "reviewersAnchor");
                    //anchor.InnerText = "Статьи на рецензирование";
                    //li.Controls.Add(anchor);
                    //Reviewers.Controls.Add(li);
                }
                else if (Roles.IsUserInRole(currentUser.UserName,"Editor"))
                {

                    liEditor.Style.Add("display", "block");
                //    LoginAuthorReviewerDiv.Visible = false;
                }
            }
            IEnumerable<DB.Item.ItemNews> news = new DB.DbNews().GetAll();
            string html = "<div></div>";
            foreach (DB.Item.ItemNews item in news)
            {
                html += String.Format("<table><tbody><tr><td><font style=\"font-size: small\">{2}</font><div style=\"width:180px\" align=\"center\"><b>{0}</b></div><div style=\"width:180px\" align=\"justify\"><i>{1}</i></div></td></tr></tbody></table><br/>", item.title, item.content, item.date.Date.ToShortDateString());
            }
            newsLabel.Text = html;
        }

        private void leftSideBar()
        {
             DB.SqlDb db = new DB.SqlDb();
              int number = 0;
              string query = "SELECT * FROM LeftSidebarBanner";
              DataTable table = db.Read(query);
              ArrayList values = new ArrayList();

              foreach (DataRow row in table.Rows)
              {
                  //vews ==1 отображаем
                  if ((int)row[0] == 1)
                  {
                      values.Add(new PositionData("~/Images/banner/" + row[1].ToString(), row[2].ToString()));
                
                  }

              }
            Repeater1.DataSource=values;
            Repeater1.DataBind();
            
                  //        <asp:HyperLink id="hyperlink1" 
                  //ImageUrl="images/pict.jpg"
                  //NavigateUrl="http://www.microsoft.com"
                  //Text="Microsoft Official Site"
                  //Target="_new"
                  //runat="server"/>    

           //HyperLink link= new HyperLink();
            
        }
        public class PositionData
        {

            private string name;
            private string url;

            public PositionData(string name,string url)
            {
                this.name = name;
                this.url = url;
            }

            public string Name
            {
                get
                {
                    return name;
                }
            }

            public string URL
            {
                get
                {
                    return url;
                }
            }
        }

        protected void HeadLoginView_ViewChanged(object sender, EventArgs e)
        {

        }

        protected void OnAdCreatedRotator(object sender, AdCreatedEventArgs e)
        {
            FlashControl1.MovieUrl = e.ImageUrl;
            FlashControl1.Base = e.NavigateUrl;
            AdRotator2.Visible = false;
        }

        protected void ToCabitenLinkClick(object sender, EventArgs e)
        {
            MembershipUser user = Membership.GetUser();
            if (user != null)
                if (Roles.IsUserInRole(user.UserName, "Reviewer"))
                {
                    Response.Redirect("/Account/ReviewerAccount/Default.aspx");
                } 
                else if(Roles.IsUserInRole(user.UserName, "Author"))
                {
                    Response.Redirect("/Account/AuthorAccount/Default.aspx");
                }
                else if(Roles.IsUserInRole(user.UserName, "Administrator"))
                {
                    Response.Redirect("/Account/ReviewerAccount/Default.aspx");
                }
                else if (Roles.IsUserInRole(user.UserName, "Editor"))
                {
                    Response.Redirect("/Account/EditorAccount/Default.aspx");
                }
        }
        public string  returnImageHeader()
        {
             DB.SqlDb db = new DB.SqlDb();
              int number = 0;
              string query = "SELECT * FROM Image_Header";
              DataTable table = db.Read(query);
              foreach (DataRow row in table.Rows)
              {
                  //vews ==1 отображаем
                  if ((int)row[0] == 1)
                  {
                      return row[1].ToString();

                  }

              }
              return "header.gif";
              
             

        }
    }
}
